package com.fingard.dsp.bank.directbank.cib02;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IInCustodyInfo;
import com.fingard.dsp.bank.reqrespobj.qryincustodyinfo.ReqQryInCustodyInfo;
import com.fingard.dsp.bank.reqrespobj.qryincustodyinfo.RetAtsInCustodyInfo;
import com.fingard.dsp.bank.reqrespobj.qryincustodyinfo.RetAtsInCustodyInfoDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 获取在押车辆信息
 * @author Parker on 2019-11-19.
 * @version 1.0
 */
public class CIB02QryInCustodyInfo extends CIB02Base implements IInCustodyInfo {
    @Override
    public RetAtsInCustodyInfo qryInCustodyInfo(ReqQryInCustodyInfo recInfo) throws Exception {
        RetAtsInCustodyInfo retAts = new RetAtsInCustodyInfo(recInfo.reqHeader);
        retAts.writeStart();
        String tmpRespCode = "";
        String tmpRespInfo = "";

        try {
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            writeCommonHeader(xmlWriter);
            xmlWriter.writeStartElement("REQUEST");
            xmlWriter.writeElementString("TRANCODE", "18003");//交易代码
            xmlWriter.writeElementString("COREID", bankFront.org);//直联企业系统客户号
            xmlWriter.writeElementString("DLENUM", recInfo.DealerCode);//经销商代码
            xmlWriter.writeElementString("CARSTU", "");//车辆状态
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(),"T18003");//发送银行
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/RESPONSE/RTNCOD");
                    String tmpRetMsg = tmpBkReader.getFirstText("/RESPONSE/RTNMSG");
                    if (StringHelper.isNullOrEmpty(tmpRetCode)) {
                        tmpRespCode = "0";
                        tmpRespInfo = "获取成功";
                        while(tmpBkReader.read("/RESPONSE/DATA")){
                            RetAtsInCustodyInfoDetail tmpRetDetail = new RetAtsInCustodyInfoDetail();
                            tmpRetDetail.DealerCode = recInfo.DealerCode;
                            tmpRetDetail.VIN = tmpBkReader.readedXElement.getSingleTextNotNull("CHANUM");
                            tmpRetDetail.PickupPrice = tmpBkReader.readedXElement.getSingleTextNotNull("CRRAMT");

                            if (!StringHelper.isNullOrEmpty(tmpBkReader.readedXElement.getSingleTextNotNull("ETYDAT"))) {
                                tmpRetDetail.CarMortgageTime = Format.DateTimeFormat.format(Format.Date8Format.parse(tmpBkReader.readedXElement.getSingleTextNotNull("ETYDAT")));
                            }
                            if (!StringHelper.isNullOrEmpty(tmpBkReader.readedXElement.getSingleTextNotNull("ENDDAT"))) {
                                tmpRetDetail.CarEneDate = Format.DateTimeFormat.format(Format.Date8Format.parse(tmpBkReader.readedXElement.getSingleTextNotNull("ENDDAT")));
                            }

                            tmpRetDetail.BillNo = tmpBkReader.readedXElement.getSingleTextNotNull("BILLNO");
                            if("1".equals(tmpBkReader.readedXElement.getSingleTextNotNull("STOTYP"))){
                                tmpRetDetail.MainRepositoryLocation = tmpBkReader.readedXElement.getSingleTextNotNull("STOARA");
                            }else{
                                tmpRetDetail.SecondNetLocation = tmpBkReader.readedXElement.getSingleTextNotNull("RTNCOD");
                            }
                            tmpRetDetail.SecondRepositoryLocation = "";
                            tmpRetDetail.CarStatus = tmpBkReader.readedXElement.getSingleTextNotNull("CARSTU");//车辆状态
                            tmpRetDetail.Reserve1 = "";
                            tmpRetDetail.Reserve2 = "";
                            tmpRetDetail.Reserve3 = "";
                            tmpRetDetail.Reserve4 = "";
                            tmpRetDetail.Reserve5 = "";
                            retAts.writeRetDetail(tmpRetDetail);
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpRetMsg;
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRespCode = "-1";
                tmpRespInfo = tmpStrRet[1];
            }

        }catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        retAts.writeRespCode(tmpRespCode);
        retAts.writeRespInfo(tmpRespInfo);
        retAts.writeEnd();
        return retAts;
    }
}
